package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.MonHocNhomTuChonM;
import net.sf.json.JSONObject;
import dao.MonHocNhomTuChonD;

@WebServlet("/MonHocNhomTuChonServlet")
public class MonHocNhomTuChonServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MonHocNhomTuChonD dao;
       
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public MonHocNhomTuChonServlet() throws Exception {
        super();
        this.dao = new MonHocNhomTuChonD();
    }
    
    /**
	 * Destruction of the servlet.
	 */
	public void destroy() {
		super.destroy();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		String flag = request.getParameter("flag");
		
		try {
			if (flag == null || "".equals(flag.trim())) {
				showMHNTC(request, response);
			}else if ("add".equals(flag.trim())) {
				String[] id_mon_hoc = request.getParameterValues("id_mon_hoc");
				String id_nhom_tu_chon = request.getParameter("id_nhom_tu_chon");
				for(int i=0;i<id_mon_hoc.length;i++){
					addMHNTC(Integer.parseInt(id_mon_hoc[i]), Integer.parseInt(id_nhom_tu_chon),request,response);
				}
				
			}else if ("remove".equals(flag.trim())) {
				String[] id_mon_hoc = request.getParameter("id").split(",");
				String[] id_nhom_tu_chon = request.getParameter("id1").split(",");
				if(id_mon_hoc.length == id_nhom_tu_chon.length){
					for(int i=0; i<id_mon_hoc.length; i++){
						deleteMHNTC(Integer.parseInt(id_mon_hoc[i]),Integer.parseInt(id_nhom_tu_chon[i]), request, response);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @function show MonHocNhomTuChon
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void showMHNTC(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String pageCount = request.getParameter("page");
		String rowsCount = request.getParameter("rows");
		String search=request.getParameter("searchTen")==null?"":request.getParameter("searchTen");
		
		int page = (pageCount == null || "".equals(pageCount)) ? 1 : Integer.valueOf(pageCount);
		int rows = (rowsCount == null || "".equals(rowsCount)) ? 10 : Integer.valueOf(rowsCount);
		
		int offSet = (page - 1) * rows;
		int allCount = dao.getAll().size();
		
		returnMap.put("total", allCount);
		returnMap.put("rows", dao.getAlls(search, offSet, rows));
		
		String returnString = JSONObject.fromObject(returnMap).toString();
		PrintWriter out = response.getWriter();
		out.println(returnString);
		out.flush();
		out.close();
	}
	
	/**
	 * @function add MonHocNhomTuChon
	 * @param id_mon_hoc
	 * @param id_nhom_tu_chon
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void addMHNTC(int id_mon_hoc, int id_nhom_tu_chon, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		MonHocNhomTuChonM mhntc = new MonHocNhomTuChonM();
		mhntc.setId_mon_hoc(id_mon_hoc);
		mhntc.setId_nhom_tu_chon(id_nhom_tu_chon);
		if(dao.Insert(mhntc)==true){
			map.put("success", "true");
			map.put("msg", "Thêm thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi thêm dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
	
	/**
	 * @function delete MonHocNhomTuChon
	 * @param id_mon_hoc
	 * @param id_nhom_tu_chon
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void deleteMHNTC(int id_mon_hoc, int id_nhom_tu_chon, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if(dao.Delete(id_mon_hoc, id_nhom_tu_chon)==true){
			map.put("success", "true");
			map.put("msg", "Xóa thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi xóa dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}

}
